import request from '../utils/request'
import tool from '../utils/tool'
import Vue from 'vue'
import FileSaver from "file-saver";
import XLSX from "xlsx";
function requests (uri, method, data, callback, errorCallback, responseType, that) {
  data = typeof data != 'object' ? {} : data
  let config = { url: uri, method: method }
  if (method == "GET") {
    config.params = data
  } else {
    config.data = data
  }
  if (responseType && typeof responseType != "undefined") {
    config.responseType = responseType
  }
  if (that && typeof that.loading == "boolean") {
    that.loading = true;
  }
  request(config).then(response => {
    if (that && typeof that.loading == "boolean") {
      that.loading = false;
    }
    if (responseType === 'blob') {
      callback(response)
      return false
    }
    if (response.code != 1000) {
      if (typeof errorCallback == "function") {
        errorCallback(response)
      }
      return false
    }

    if (typeof callback == "function") {
      callback(response)
    }
  }).catch(error => {
    if (typeof errorCallback == "function") {
      errorCallback()
    }
    // console.log(error)
  })
}

let apis = {

  base_url: 'http://shenleyou.cqxishui.top/',
  root_url: 'http://shenleyou.cqxishui.top/',
  upload_img_url: 'http://shenleyou.cqxishui.top/uploadsImages',
/*    base_url: 'http://shenleyou.cqxishui.top/admin/',
    root_url: 'http://shenleyou.cqxishui.top/admin/',
    upload_img_url: 'http://shenleyou.cqxishui.top/upload-image',*/
  //七牛云资源访问域名
  qiniuAsset: '',
  qiniuUpload: '',
  requests: requests,
  get (uri, data, callback, errorCallback, responseType, that) {
    requests(uri, 'GET', data, callback, errorCallback, responseType, that)
  },
  post (uri, data, callback, errorCallback, responseType, that) {
    requests(uri, 'POST', data, callback, errorCallback, responseType, that)
  },
  put (uri, data, callback, errorCallback, responseType, that) {
    requests(uri, 'PUT', data, callback, errorCallback, responseType, that)
  },
  delete (uri, data, callback, errorCallback, responseType, that) {
    requests(uri, 'DELETE', data, callback, errorCallback, responseType, that)
  },
  /**
   * 登录
   * @param {object} data 
   * @param {function} callback 
   * @param {function} errorCallback 
   */
  login: function (data, callback, errorCallback) {
    requests('login', 'POST', data, function (response) {
      if (response.code == 1000) {
        Vue.$cookies.set('adminInfo', response.data.info)
      }
      callback(response)
    }, errorCallback)
  },

  /**
   * 导出用户
   * @param {*} callback 
   */
  exportUser (callback) {
    requests('exportUser', 'GET', {}, function (response) {
      console.log(response)
      callback(response)
    }, null, 'blob')
  },

  /***
   * 获取管理员信息
   */
  getAdminInfo: function (callback) {
    requests('getAdminInfo', 'GET', {}, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },

  /***
   * 修改管理员信息
   */
  updateAdmins: function (data, callback) {
    requests('updateAdmins', 'PUT', data, function (response) {
      if (response.code == 1000) {
        Vue.$cookies.set('adminInfo', response.data)
      }
      callback(response)
    })
  },


  /***
   * 获取api列表
   */
  getApi: function (data, callback) {
    requests('getApi', 'GET', data, function (response) {
      if (response.code != 1000) {
        // Vue.$message.error(response.msg);
        return false
      }
      callback(response)
    })
  },

  /***
   * 添加api
   */
  addApi: function (data, callback) {
    requests('addApi', 'POST', data, function (response) {
      callback(response)
    })
  },


  /***
   * 编辑api
   */
  editApi: function (data, callback) {
    requests('editApi', 'PUT', data, function (response) {
      callback(response)
    })
  },


  /***
   * 删除api
   */
  delApi: function (ids, callback) {
    requests('delApi', 'DELETE', { ids: ids }, function (response) {
      callback(response)
    })
  },


  /***
   * 获取权限角色列表
   */
  getRole: function (data, callback) {
    requests('getRole', 'GET', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 删除角色
   */
  delRole: function (ids, callback) {
    requests('delRole', 'DELETE', { ids: ids }, function (response) {
      callback(response)
    })
  },

  /***
   * 添加角色
   */
  addRole: function (data, callback) {
    requests('addRole', 'POST', data, function (response) {
      callback(response)
    })
  },

  /***
   * 编辑操作
   */
  editRole: function (data, callback) {
    requests('editRole', 'PUT', data, function (response) {
      callback(response)
    })
  },

  /***
   * 获取管理员列表
   */
  getAdmins: function (data, callback) {
    requests('getAdmins', 'GET', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
  * 添加管理员
  */
  addAdmins: function (data, callback) {
    requests('addAdmins', 'POST', data, function (response) {
      callback(response)
    })
  },

  /***
  * 修改管理员
  */
  editAdmins: function (data, callback) {
    requests('editAdmins', 'PUT', data, function (response) {
      callback(response)
    })
  },

  /***
   * 删除管理员
   */
  delAdmins: function (ids, callback) {
    requests('delAdmins', 'DELETE', { ids: ids }, function (response) {
      callback(response)
    })
  },

  /***
   * 获取banner列表
   */
  getDayType: function (data, callback) {
    requests('getDayType', 'GET', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 添加banner
   */
  addDayType: function (data, callback) {
    requests('addDayType', 'POST', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 编辑banner
   */
  editDayType: function (data, callback) {
    requests('editDayType', 'PUT', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 删除banner
   */
  delDayType: function (ids, callback) {
    requests('delDayType', 'DELETE', { ids: ids }, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },

  /***
   * 获取banner列表
   */
  getDayTask: function (data, callback) {
    requests('getDayTask', 'GET', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 添加banner
   */
  addDayTask: function (data, callback) {
    requests('addDayTask', 'POST', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 编辑banner
   */
  editDayTask: function (data, callback) {
    requests('editDayTask', 'PUT', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 删除banner
   */
  delDayTask: function (ids, callback) {
    requests('delDayTask', 'DELETE', { ids: ids }, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },

  /***
 * 获取banner列表
 */
  getBanner: function (data, callback) {
    requests('getBanner', 'GET', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 添加banner
   */
  addBanner: function (data, callback) {
    requests('addBanner', 'POST', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 编辑banner
   */
  editBanner: function (data, callback) {
    requests('editBanner', 'PUT', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
  * 删除banner
  */
  delBanner: function (ids, callback) {
    requests('delBanner', 'DELETE', { ids: ids }, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },





  /***
  * 获取全部地区数据
  */
  getAreasAll: function (callback) {
    requests('getAreasAll', 'GET', {}, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data.list)
    })
  },

  /***
   * 删除地区数据
   */
  delAreas (ids, callback) {
    requests('delAreas', 'DELETE', { ids: ids }, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },

  /***
 * 获取公告列表
 */
  getNotice: function (data, callback) {
    requests('getNotice', 'GET', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 添加公告
   */
  addNotice: function (data, callback) {
    requests('addNotice', 'POST', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
   * 编辑公告
   */
  editNotice: function (data, callback) {
    requests('editNotice', 'PUT', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },

  /***
  * 删除公告
  */
  delNotice: function (ids, callback) {
    requests('delNotice', 'DELETE', { ids: ids }, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },
    /***
     * 获取公告列表
     */
    getUserRed: function (data, callback) {
        requests('getUserRed', 'GET', data, function (response) {
            if (response.code != 1000) {
                return false
            }
            callback(response.data)
        })
    },
    /**
     * 添加红包
     * @param data
     * @param callback
     */
    addUserRed: function (data, callback) {
        requests('addUserRed', 'POST', data, function (response) {
            if (response.code != 1000) {
                return false
            }
            callback(response.data)
        })
    },
    /***
     * 编辑红包
     */
    editUserRed: function (data, callback) {
        requests('editUserRed', 'PUT', data, function (response) {
            if (response.code != 1000) {
                return false
            }
            callback(response.data)
        })
    },

    /***
     * 删除红包
     */
    delUserRed: function (ids, callback) {
        requests('delUserRed', 'DELETE', { ids: ids }, function (response) {
            if (response.code != 1000) {
                return false
            }
            callback(response)
        })
    },




  /**
   * 导出财务记录
   * @param {object} query 
   * @param {function} callback 
   */
  exportFinanceRecord (query, callback) {
    requests('exportFinanceRecord', 'GET', query, function (response) {
      callback(response)
    }, null, 'blob')
  },



  /**
   * 首页数据
   * @param {function} callback 
   */
  home: function (callback) {
    requests('home', 'GET', {}, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })

  },

  /**
   * 刷新首页数据
   * @param {function} callback 
   */
  refreshHome (callback) {
    requests('refreshHome', 'GET', {}, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },


  /**
   * 获取邮箱配置
   * @param {function} callback 
   */
  getEmailConfig: function (callback) {
    requests('getEmailConfig', 'GET', {}, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data.info)
    })
  },

  /**
 * 编辑邮箱配置
 * @param {object} data 
 * @param {function} callback 
 */
  editEmailConfig: function (data, callback) {
    requests('editEmailConfig', 'PUT', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response.data)
    })
  },
  /**
   * 测试邮箱发送
   * @param {object} data 
   * @param {function} callback 
   */
  testEmailSend: function (data, callback) {
    requests('testEmailSend', 'POST', data, function (response) {
      if (response.code != 1000) {
        return false
      }
      callback(response)
    })
  },
  exportExcel(table,name) {
    /* 从表生成工作簿对象 */
    let wb = XLSX.utils.table_to_book(table)//document.querySelector('#out-table')
    /* 获取二进制字符串作为输出 */
    var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
    })
    try {
        FileSaver.saveAs(
        //Blob 对象表示一个不可变、原始数据的类文件对象。
        //Blob 表示的不一定是JavaScript原生格式的数据。
        //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
        //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
        new Blob([wbout], { type: 'application/octet-stream' }),
        //设置导出文件名称
        name+'.xlsx'
        )
    } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
    }
    return wbout
    }
}








export default apis